using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace MicroMsg.UI.UserContrl
{
	public class ChatVoiceItem : BaseChatElement
	{
		private const int MinLen = 1;
		private const int MaxLen = 60;
		private const int DefaultLen = 1;
		private const int MinElementWidth = 62;
		private const int MaxElementWidth = 280;
		private const int InValidVoiceLen = -100;
		private StackPanel ContentPanel;
		private TextBlock TimeTxt;
		private Image ProgressImg;
		private new BaseChatContent ChatContentCtrl;
		public static DependencyProperty UnReadFlagVisibilityProperty = DependencyProperty.Register("UnReadFlagVisibility", typeof(Visibility), typeof(ChatVoiceItem), new PropertyMetadata(1, new PropertyChangedCallback(ChatVoiceItem.OnUnReadFlagVisibilityChanged)));
		private int mVoiceLen = -100;
		private int progressFlagIndex;
		public Visibility UnReadFlagVisibility
		{
			get
			{
				return (Visibility)base.GetValue(ChatVoiceItem.UnReadFlagVisibilityProperty);
			}
			set
			{
				base.SetValue(ChatVoiceItem.UnReadFlagVisibilityProperty, value);
			}
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.ContentPanel = (base.GetTemplateChild("ContentPanel") as StackPanel);
			this.ProgressImg = (base.GetTemplateChild("ProgressImg") as Image);
			this.TimeTxt = (base.GetTemplateChild("TimeTxt") as TextBlock);
			this.ChatContentCtrl = (base.GetTemplateChild("ChatContentCtrl") as BaseChatContent);
			this.ChatContentCtrl.add_Click(new RoutedEventHandler(this.ContentPanel_Click));
			this.RefreshVoiceLen();
			this.SetTimeTxt();
			this.RefreshProgress();
			this.RefreshGlittleing();
		}
		private void ContentPanel_Click(object sender, RoutedEventArgs e)
		{
			if (this.mclickEvent != null)
			{
				this.mclickEvent(this.chatMsg_0, this);
			}
		}
		private void RefreshProgress()
		{
			if (this.ContentPanel == null)
			{
				return;
			}
			if (this.ProgressImg == null)
			{
				return;
			}
			if (this.progressFlagIndex == 1)
			{
				this.ProgressImg.set_Source(null);
				return;
			}
			int num;
			if (this.progressFlagIndex > 1)
			{
				num = this.progressFlagIndex - 1;
			}
			else
			{
				num = this.progressFlagIndex;
			}
			string text;
			if (this is ChatLeftVoice)
			{
				text = string.Format("/Resource/Img/Chat/voiceflag/left{0}.png", num);
			}
			else
			{
				if (this is ChatRightVoice)
				{
					text = string.Format("/Resource/Img/Chat/voiceflag/right{0}.png", num);
				}
				else
				{
					if (this is DriftBottleVoiceItem)
					{
						text = string.Format("/Source/Plugin/DriftBottle/Resource/Img/voice0{0}.png", num + 1);
					}
					else
					{
						text = "";
					}
				}
			}
			this.ProgressImg.set_Source(new ImageSourceConverter().ConvertFromString(text) as ImageSource);
		}
		private double getVoiceItemLen()
		{
			this.mVoiceLen = Math.Max(this.mVoiceLen, 1);
			this.mVoiceLen = Math.Min(this.mVoiceLen, 60);
			double num = 62.0;
			double num2 = 171.0;
			double num3 = (num2 - num) / 8.0;
			double num4 = (280.0 - num2) / 5.0;
			if (this.mVoiceLen <= 2)
			{
				return num;
			}
			if (this.mVoiceLen <= 10)
			{
				return (double)(this.mVoiceLen - 2) * num3 + num;
			}
			return (double)((this.mVoiceLen - 10) / 10) * num4 + num2;
		}
		private void RefreshVoiceLen()
		{
			if (this.ContentPanel == null)
			{
				return;
			}
			if (this.mVoiceLen == -100)
			{
				return;
			}
			double voiceItemLen = this.getVoiceItemLen();
			if (this.ContentPanel.get_ActualWidth() == 0.0 || this.chatMsg_0.GetStatus() != MsgUIStatus.Processing)
			{
				this.ContentPanel.set_Width(voiceItemLen);
			}
		}
		private void SetTimeTxt()
		{
			if (this.TimeTxt == null)
			{
				return;
			}
			this.TimeTxt.set_Text(this.mVoiceLen + "\"");
		}
		private Storyboard getWidthStoryBoard(double startW, double endW)
		{
			TimeSpan timeSpan = TimeSpan.FromSeconds(0.5);
			Storyboard storyboard = new Storyboard();
			DoubleAnimation doubleAnimation = new DoubleAnimation();
			doubleAnimation.set_From(new double?(startW));
			doubleAnimation.set_To(new double?(endW));
			doubleAnimation.set_Duration(timeSpan);
			DoubleAnimation doubleAnimation2 = doubleAnimation;
			Storyboard.SetTarget(doubleAnimation2, this.ContentPanel);
			Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath(FrameworkElement.WidthProperty));
			storyboard.get_Children().Add(doubleAnimation2);
			return storyboard;
		}
		public void SetPlayingStatus(bool isPlaying)
		{
			if (this.ChatContentCtrl == null)
			{
				return;
			}
			this.ChatContentCtrl.IsDeleteEnable = !isPlaying;
		}
		private static void OnUnReadFlagVisibilityChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		public override void SetUnReadFlag(MsgReadStatus status)
		{
			Log.smethod_5("Voice", string.Concat(new object[]
			{
				"msg id",
				this.chatMsg_0.nMsgLocalID,
				"  ",
				status
			}));
			if (status == MsgReadStatus.MsgUnRead)
			{
				this.UnReadFlagVisibility = 0;
				return;
			}
			this.UnReadFlagVisibility = 1;
		}
		public override void SetVoiceLen(int voiceLen)
		{
			this.mVoiceLen = voiceLen;
			this.RefreshVoiceLen();
			this.SetTimeTxt();
		}
		public void ProgressFlagChange()
		{
			if (this.progressFlagIndex >= 4)
			{
				this.progressFlagIndex = 1;
			}
			else
			{
				this.progressFlagIndex++;
			}
			this.RefreshProgress();
		}
		public void EndProgressChange()
		{
			this.progressFlagIndex = 0;
			this.RefreshProgress();
		}
		private void RefreshGlittleing()
		{
			if (this.ChatContentCtrl == null)
			{
				return;
			}
			if (base.IsGlittleing)
			{
				this.ChatContentCtrl.IsGlitterFlag = true;
				return;
			}
			this.ChatContentCtrl.IsGlitterFlag = false;
		}
		protected override void GlittleingChange(bool newValue)
		{
			this.RefreshGlittleing();
		}
	}
}
